package com.kucai.yifaqapp.ui.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.Pay_DanYICZBean;
import com.kucai.yifaqapp.model.network.entity.res.z_cft_ty;
import com.kucai.yifaqapp.util.SYSOUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by admin on 2018/12/4.
 */

public class Z_cftxinctivity extends BaseActivitySimple {
    LinearLayout skinbg;
    TextView remark2;
    TextView payee;
    TextView remark;
    TextView dsplay;
    Button next;
    private List<String> mdatas = new ArrayList<>();
    Pay_DanYICZBean bean;
    RelativeLayout back;
    ImageView dizhi;
    private OptionsPickerView pvOptions;
    Button copy1;
    Button copy2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        back = findViewById(R.id.back);
        dizhi = findViewById(R.id.dizhi);
        skinbg = findViewById(R.id.skinbg);
        copy1 = findViewById(R.id.copy1);
        copy2 = findViewById(R.id.copy2);
        copy1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager copy = (ClipboardManager) Z_cftxinctivity.this
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                copy.setText(remark2.getText().toString());
                Toast.makeText(getApplicationContext(), "复制成功", Toast.LENGTH_SHORT).show();
            }
        });
        copy2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager copy = (ClipboardManager) Z_cftxinctivity.this
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                copy.setText(payee.getText().toString());
                Toast.makeText(getApplicationContext(), "复制成功", Toast.LENGTH_SHORT).show();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        try {
            initView();
            initData();
        } catch (Exception e) {

        }
    }

    @Override
    protected int setContentView() {
        return R.layout.z_cft;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }


    private void initView() {
        remark2 = findViewById(R.id.remark2);
        payee = findViewById(R.id.payee);
        remark = findViewById(R.id.remark);
        dsplay = findViewById(R.id.dsplay);
        next = findViewById(R.id.next);
    }


    private void initData() {
        HttpJsonUtils.getHttpApiInstance()
                .getSingleCZ(HttpAdress.getuseAdress() + "api/m/user/getPay", "cft", Info.user.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Pay_DanYICZBean>(this) {
                    @Override
                    protected void onCustomNext(Pay_DanYICZBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                bean = resBanner;
                                for (int i = 0; i < resBanner.getRech_types().size(); i++) {
                                    mdatas.add(resBanner.getRech_types().get(i).getDsplay());
                                }
                                //给spinner设置adapter
//                                ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(Z_cftxinctivity.this,
//                                        R.layout.item_selected, mdatas);
//                                spinnerAdapter.setDropDownViewResource(R.layout.item_selected);
//                                dsplay.setAdapter(spinnerAdapter);
//                                dsplay.setDropDownWidth(600); //下拉宽度
//                                dsplay.setDropDownVerticalOffset(1); //下拉的纵向偏移
//                                dsplay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//                                    @Override
//                                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                                        TextView tv = (TextView) view;
//                                        tv.setTextColor(getResources().getColor(R.color.white));    //设置颜色
//                                        tv.setTextSize(13.0f);    //设置大小
//                                        tv.setGravity(Gravity.CENTER_VERTICAL);   //设置居中
//                                        tv.setPadding(50, 0, 0, 0);
//                                        remark2.setText(bean.getRech_types().get(position).getRemark2() + "");
//                                        payee.setText(bean.getRech_types().get(position).getPayee() + "");
//                                        remark.setText(bean.getRech_types().get(position).getRemark() + "");
//                                        Glide.with(Z_cftxinctivity.this).load(bean.getRech_types().get(position).getQrcode()).into(dizhi);
//                                        z_cft_ty.cfgId = bean.getRech_types().get(position).getId() + "";
//                                        z_cft_ty.yinghang = mdatas.get(position) + "";
//                                        z_cft_ty.wangdian = bean.getRech_types().get(position).getPageDesc() + "";
//                                        z_cft_ty.xingming = bean.getRech_types().get(position).getRemark2() + "";
//                                        z_cft_ty.kahao = bean.getRech_types().get(position).getPayee() + "";
//                                        z_cft_ty.max = bean.getRech_types().get(position).getMax_money() + "";
//                                        z_cft_ty.min = bean.getRech_types().get(position).getMin_money() + "";
//                                    }
//
//                                    @Override
//                                    public void onNothingSelected(AdapterView<?> parent) {
//                                    }
//                                });
                                pvOptions = new OptionsPickerView.Builder(Z_cftxinctivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                                    @Override
                                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                                        remark2.setText(bean.getRech_types().get(options1).getPayeeName() + "");
                                        payee.setText(bean.getRech_types().get(options1).getPayee() + "");
                                        remark.setText(bean.getRech_types().get(options1).getRemark() + "");
                                        dsplay.setText(mdatas.get(options1) + "");
                                        Glide.with(Z_cftxinctivity.this).load(bean.getRech_types().get(options1).getQrcode()).into(dizhi);
                                        z_cft_ty.cfgId = bean.getRech_types().get(options1).getId() + "";
                                        z_cft_ty.yinghang = mdatas.get(options1) + "";
                                        z_cft_ty.wangdian = bean.getRech_types().get(options1).getPageDesc() + "";
                                        z_cft_ty.xingming = bean.getRech_types().get(options1).getRemark() + "";
                                        z_cft_ty.kahao = bean.getRech_types().get(options1).getPayee() + "";
                                        z_cft_ty.max = bean.getRech_types().get(options1).getMax_money() + "";
                                        z_cft_ty.min = bean.getRech_types().get(options1).getMin_money() + "";
                                    }
                                })
                                        .setSubCalSize(20)//确定和取消文字大小
                                        .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                                        .setCancelColor(Color.BLUE)//取消按钮文字颜色
                                        .setTextColorCenter(Color.BLACK)//设置选中项的颜色
                                        .setSelectOptions(0)
                                        .build();
                                pvOptions.setPicker(mdatas);
                                dsplay.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        pvOptions.show();
                                    }
                                });


                                try {
                                    remark2.setText(bean.getRech_types().get(0).getPayeeName() + "");
                                    payee.setText(bean.getRech_types().get(0).getPayee() + "");
                                    remark.setText(bean.getRech_types().get(0).getRemark() + "");
                                    dsplay.setText(mdatas.get(0) + "");
                                    Glide.with(Z_cftxinctivity.this).load(bean.getRech_types().get(0).getQrcode()).into(dizhi);
                                    z_cft_ty.cfgId = bean.getRech_types().get(0).getId() + "";
                                    z_cft_ty.yinghang = mdatas.get(0) + "";
                                    z_cft_ty.wangdian = bean.getRech_types().get(0).getPageDesc() + "";
                                    z_cft_ty.xingming = bean.getRech_types().get(0).getRemark() + "";
                                    z_cft_ty.kahao = bean.getRech_types().get(0).getPayee() + "";
                                    z_cft_ty.max = bean.getRech_types().get(0).getMax_money() + "";
                                    z_cft_ty.min = bean.getRech_types().get(0).getMin_money() + "";
                                } catch (Exception e) {

                                }
                            } else {
                                Toast.makeText(Z_cftxinctivity.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            Toast.makeText(Z_cftxinctivity.this, "异常错误，请重新操作", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dsplay.getText().toString().equals("请点击选择支付账号")) {
                    Toast.makeText(Z_cftxinctivity.this, "请点击选择支付账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(Z_cftxinctivity.this, Z_cftxinctivityTwo.class);
                Bundle bundle = new Bundle();
                bundle.putString("remark", remark.getText().toString() + "");
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });
    }
}
